/**
 * Created with IntelliJ IDEA.
 * Description:
 * https://gitee.com/li--jiaqiang/java-test.git
 * git pull
 * git pull origin master
 * git pull origin master --allow-unrelated-histories
 * User: 86187
 * Date: 2022-05-06
 * Time: 23:44
 */

//Scanner scan = new Scanner(System.in);


import java.util.Arrays;

public class TestDemo {

    /**
     * 二分查找，找到返回true,否则返回false
     * @param array
     * @return
     */
    public static boolean myBinarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length - 1;

        while(left <= right) {
            int mid = (left + right) / 2;
            if(array[mid] == key) {
                return true;
            } else if(array[mid] < key) {
                left = mid + 1;
            } else {
                right = mid - 1;
            }
        }
        return false;
    }

    public static void main(String[] args) {
        int[] array = {1,5,8,12,25,30};

        System.out.println(myBinarySearch(array, 13));
    }


    /**
     * 判断数组是否是升序，是返回true，否则返回false
     * @param array
     * @return
     */
    public static boolean judgeArray(int[] array) {
        if(array == null) {
            return false;
        }
        if(array.length == 1) {
            return true;
        }
        int p = 0;
        int q = 1;
        while(q < array.length) {
            if(array[p] > array[q]) {
                return false;
            }
            p++;
            q++;
        }
        return true;
    }

    public static void main5(String[] args) {
        int[] array = {1,6,8,10,16};

        System.out.println(judgeArray(array));
    }



    public static void main4(String[] args) {
        int[] array = {5,2,15,7,25,34,28};
        boolean flg = true;
        for (int i = 0; i < array.length - 1; i++) {
            for (int j = i+1; j < array.length; j++) {
                if(array[i] > array[j]) {
                    int tmp = array[i];
                    array[i] = array[j];
                    array[j] = tmp;
                    flg = false;
                }
            }
            if(flg) {
                break;
            }
        }
        System.out.println(Arrays.toString(array));
    }

    public static int[] search(int[] array ,int target) {
        int[] ret = new int[2];
        int m = 0;
        for(int i = 0;i<array.length-1;++i) {
            for(int j = i+1;j < array.length;++j) {
                if(array[i] + array[j] == target) {
                    ret[0] = i;
                    ret[1] = j;
                    m = 1;
                    break;
                }
            }
            if(m == 1) {
                break;
            }
        }
        return ret;
    }

    public static void main3(String[] args) {
        int[] array = {2,7,11,15};
        System.out.println(Arrays.toString(search(array, 9)));
    }

    public static void main2(String[] args) {
        int[] array = new int[100];
        for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        System.out.println(Arrays.toString(array));
    }

    public static int[] transform (int[] array) {
        if(array == null) {
            return null;
        }

        for (int i = 0; i < array.length; i++) {
            array[i] = array[i] * 2;
        }
        return array;
    }
    public static void main1(String[] args) {
        int[] array = {1,2,3};
        System.out.println(Arrays.toString(transform(array)));
    }
}
